package fun.xming.memlib;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import fun.xming.memlib.tools.UnitConversion;

public class ReviewMemActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reviewmem);
		MainActivity.getMemManager().refreshContents();
        HashMap priviews = MainActivity.getMemManager().getMemPreviews();
        for(Object o:priviews.entrySet()) {
            Map.Entry<String,HashMap> entry = (Map.Entry<String,HashMap>)o;
            LinearLayout l = createCard();
            ((TextView)(l.getChildAt(0))).setText(entry.getKey());
//            "create-time" => <Int>
//     *      "example" => <String>
//     *      "item-count" => <int>,
//     *      "mem-keep-index" => <int>,
            String str = "Eg: "+entry.getValue().get("example")
                    +"etc  ["+entry.getValue().get("item-count")+ "] in totil.\n"
                    +" Created at "+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(String.valueOf(entry.getValue().get("create-time")))));
            ((TextView)(l.getChildAt(1))).setText(str);
			//MainActivity.alert(""+entry.getValue().get("mem-keep-index"));
            ((ProgressBar)(((LinearLayout)(l.getChildAt(2))).getChildAt(0))).setProgress(Integer.valueOf(""+entry.getValue().get("mem-keep-index")));
            LinearLayout main = (LinearLayout)findViewById(R.id.revmem_main);
            main.addView(l,main.getChildCount());
        }
    }

    public void MemCardOnClick(View view) {
        String memName = String.valueOf(((TextView)(((LinearLayout)view).getChildAt(0))).getText());
        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("To do...");   // add a list
        String[] animals = {"(Re)learn", "Delete"};
        builder.setItems(animals, new DialogInterface.OnClickListener() {
            private String memName;
            private ReviewMemActivity context;
            public DialogInterface.OnClickListener setContext (String n,ReviewMemActivity c) {
                memName = n;
                context = c;
                return this;
            }
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        Intent intent = new Intent(ReviewMemActivity.this, RelearnActivity.class);
                        intent.putExtra("name",memName);
                        startActivity(intent);
                        break;
                    case 1:
                        // setup the alert builder
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
                        builder2.setTitle("Sure to DELETE ?_?");   // add a list
                        String[] animals = {"Certainly", "No f*k"};
                        builder2.setItems(animals, new DialogInterface.OnClickListener() {
                            private String memName;
                            private ReviewMemActivity context;
                            public DialogInterface.OnClickListener setContext (String n,ReviewMemActivity c) {
                                memName = n;
                                context = c;
                                return this;
                            }
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:
                                        MainActivity.getMemManager().dropMem(memName);
                                        context.onCreate(null);
                                        MainActivity.alert("Delete done.");
                                }
                            }
                        }.setContext(memName,context));
// create and show the alert dialog
                        AlertDialog dialog2 = builder2.create();
                        dialog2.show();
                }
            }
        }.setContext(memName,this));
// create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private LinearLayout createCard() {
        LinearLayout l = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.card_review_mem,null,false);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(UnitConversion.dipToPx(this,280),UnitConversion.dipToPx(this,104));
        l.setPadding(UnitConversion.dipToPx(this,10),UnitConversion.dipToPx(this,5),UnitConversion.dipToPx(this,10),UnitConversion.dipToPx(this,5));
        l.setLayoutParams(lp);
        return l;
    }
    
}
